using System;
using System.Globalization;
using System.Web.UI.WebControls;

namespace Sedna.Core.Controller.Configurations
{
	/// <summary>
	/// Summary description for RangeValidatorConfiguration.
	/// </summary>
	public class RangeValidatorConfiguration: ValidatorConfiguration, IRangeValidatorConfiguration
	{
		private string minimumValue;
		private string maximumValue = string.Empty;

        private string minimumValueBindPath = null;
        private string maximumValueBindPath = null;
        private string minimumValueUnbindPath = null;
        private string maximumValueUnbindPath = null;

        private ValidationDataType type = ValidationDataType.String;

		public string MinimumValue
		{
			get { return GetMinimumValue(); }
			set { minimumValue = value; }
		}

		public string MaximumValue
		{
			get { return GetMaximumValue(); }
			set { maximumValue = value; }
		}
	    
	    public string MinimumValueBindPath
        {
            get
            {
                return this.minimumValueBindPath;
            }
            set
            {
                this.minimumValueBindPath = value;
            }
        }

        public string MinimumValueUnbindPath
        {
            get
            {
                return this.minimumValueUnbindPath;
            }
            set
            {
                this.minimumValueUnbindPath = value;
            }
        }
	    
	    public string MinimumValuePath
	    {
	        set
	        {
                this.minimumValueBindPath = value;
                this.minimumValueUnbindPath = value;	            
	        }
	    }

        public string MaximumValueUnbindPath
        {
            get
            {
                return this.maximumValueUnbindPath;
            }
            set
            {
                this.maximumValueUnbindPath = value;
            }
        }

        public string MaximumValueBindPath
        {
            get
            {
                return this.maximumValueBindPath;
            }
            set
            {
                this.maximumValueBindPath = value;
            }
        }

        public string MaximumValuePath
        {
            set
            {
                this.maximumValueBindPath = value;
                this.maximumValueUnbindPath = value;
            }
        }


		public ValidationDataType Type
		{
			get { return type; }
			set { type = value; }
		}

		/// <summary>
		/// Formats values according to current culture. 
		/// </summary>
		/// <param name="value">Value formatted according to invariant culture.</param>
		/// <returns>Value formatted according to current culture.</returns>
		private string TranslateToCurrentCulture(string value)
		{
			if (type==ValidationDataType.Integer||type==ValidationDataType.String)
			{
				return value;
			}	

			switch (type)
			{
				case ValidationDataType.Currency:	return Decimal.Parse(value,NumberStyles.Currency, CultureInfo.InvariantCulture).ToString(PrincipalWrapper.Culture);
                case ValidationDataType.Date:
			        return DateTime.Parse(value, CultureInfo.InvariantCulture).ToShortDateString();//.ToString(PrincipalWrapper.Culture);
				case ValidationDataType.Double:		return Double.Parse(value, NumberStyles.Number, CultureInfo.InvariantCulture).ToString(PrincipalWrapper.Culture);			
			}

			return value;
		}
	    
	    private string GetMinimumValue()
	    {
            if (minimumValue != null && !String.Empty.Equals(minimumValue.Trim()))
	        {
                return TranslateToCurrentCulture(minimumValue);
	        }
            switch (Type)
            {
                case ValidationDataType.Currency: return Decimal.MinValue.ToString(PrincipalWrapper.Culture);
                case ValidationDataType.Date:
                    return DateTime.MinValue.ToShortDateString();//.ToString(PrincipalWrapper.Culture);
                case ValidationDataType.Double: return Decimal.MinValue.ToString(PrincipalWrapper.Culture);
                case ValidationDataType.Integer: return Int32.MinValue.ToString(PrincipalWrapper.Culture);
            }
            return "";
        }
	    
	    private string GetMaximumValue()
	    {
            if (maximumValue != null && !String.Empty.Equals(maximumValue.Trim()))
            {
                return TranslateToCurrentCulture(maximumValue);
            }
	        switch (Type)
	        {
	            case ValidationDataType.Currency: return Decimal.MaxValue.ToString(PrincipalWrapper.Culture);
	            case ValidationDataType.Date: return DateTime.MaxValue.ToShortDateString();//.ToString(PrincipalWrapper.Culture);
	            case ValidationDataType.Double: return Decimal.MaxValue.ToString(PrincipalWrapper.Culture);
                case ValidationDataType.Integer: return Int32.MaxValue.ToString(PrincipalWrapper.Culture);
            }
            return "";
        }

	}
}
